package com.fansl.allround.message.listenner;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.fansl.allround.message.cache.ClientCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/9/5 10:46
 */
@Slf4j
@Component
public class EventListener {
    @Resource
    private ClientCache clientCache;

    /**
     * 客户端连接
     * @param client
     */
    @OnConnect
    public void onConnect(SocketIOClient client) {
        String userId = client.getHandshakeData().getSingleUrlParam("userId");
        if(userId==null){
            return;
        }
        UUID sessionId = client.getSessionId();
        clientCache.saveClient(userId,sessionId,client);
        log.info(client.getRemoteAddress()+" web客户端接入");
    }

    /**
     * 客户端断开
     * @param client
     */
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        String userId = client.getHandshakeData().getSingleUrlParam("userId");
        if(userId==null){
            return;
        }
        clientCache.deleteSessionClient(userId,client.getSessionId());
        log.info(client.getRemoteAddress()+" web客户端断开");
    }
}
